﻿using B.S.DataBase.MyProject.API.Appicant.Command;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.UserCommandHandler
{
    public class LoginCommandHandler : IRequestHandler<LoginCommand, APIResult<User>>
    {
        private readonly IBaseRepository<User> userRepos;

        public LoginCommandHandler(IBaseRepository<User> userRepos)
        {
            this.userRepos = userRepos;
        }


        public Task<APIResult<User>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            APIResult<User> result = new APIResult<User>();

            var login = userRepos.GetAll().Result.FirstOrDefault(x => x.UserName == request.UserName && x.Password == request.Password);
            if (login == null)
            {
                result.Code = APIEnum.登录失败密码或用户名错误;
                result.Msg = APIEnum.登录失败密码或用户名错误.ToString();
            }
            else
            {
                result.Code = APIEnum.登录成功;
                result.Msg = APIEnum.登录成功.ToString();
                result.data = login;
            }

            return Task.FromResult(result);

        }
    }
}
